#include "mushroom.h"
#include <vector>
#include <typeinfo>

Mushroom::Mushroom(GameGraphic* pGraphic, QuadTree<GameObject>* quadtree, const char* nameObj, int _type, float x, float y, int layer)
	: DynamicObject(pGraphic, nameObj, x, y, layer)
{	
    worldMap = quadtree;
    move = 0.3;
    type = _type;
}

void Mushroom::Update(unsigned int gameTime)
{	
    Rect prevBound = this->GetRectBound();
    posX += move;
    posY += 4;
    worldMap->RemoveObject(this);
    vector<GameObject*> temp = worldMap->GetObjects(prevBound.left, prevBound.top, (this->GetRectBound()).right, (this->GetRectBound()).bottom);
    for (vector<GameObject*>::iterator i = temp.begin(); i != temp.end(); ++i)
    {	
        if ((typeid(**i) == typeid(DynamicObject)) && IsCollide((*i)->GetRectBound(), this->GetRectBound()))
        {	
            if (prevBound.bottom <= ((*i)->GetRectBound()).top)
                posY = ((*i)->GetRectBound()).top - pSprite->GetHeight();
            else if (prevBound.top >= ((*i)->GetRectBound()).bottom)
                posY = ((*i)->GetRectBound()).bottom;
            else if (prevBound.right <= ((*i)->GetRectBound()).left)
            {	
                posX = ((*i)->GetRectBound()).left - pSprite->GetWidth();
                move = -0.3;
            }
            else if (prevBound.left >= ((*i)->GetRectBound()).right)
            {	
                posX = ((*i)->GetRectBound()).right;
                move = 0.3;
            }
        }
    }
    worldMap->InsertObject(this);
    return;
}
